(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("mglService", mglService);
	mglService.$inject = [ "$q", "$filter", "ajaxServiceFactory", "modalService", "arithService" ];
	function mglService($q, $filter, ajaxServiceFactory, modalService, arithService) {

		function initDataOptions() {
			var ajaxServices = ajaxServiceFactory.buildAjaxService({
				getMglCreateOptions : {
					url : "do/cls/dataList/getMglCreateOptions",
					method : "get"
				}
			});
			var defer = $q.defer();
			ajaxServices.getMglCreateOptions().then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		}
		
		function getLoanInfoByLoanId(loanId){
			var ajaxServices = ajaxServiceFactory.buildAjaxService({
				getLoanInfoByLoanId : {
					url : "do/cls/loan/getLoanInfoByLoanId",
					method : "get"
				}
			});
			var defer = $q.defer();
			ajaxServices.getLoanInfoByLoanId({"loanId" : loanId}).then(function(response) {
				defer.resolve(response ? response.data : {});
			});
			return defer.promise;
		}

		function resetMglentrydetails(mglentrydetails) {
			if (mglentrydetails) {
				angular.forEach(mglentrydetails, function(row) {
					row.custCode = undefined;
					row.baseCcy = undefined;
					row._acctNoConfig = undefined;
					row.custAcctFlag = undefined;
					row._custAcctNo = undefined;
				});
			}
		}

		function glAmtChanged(row, flag, mgl) {
			if (flag || !mgl.fxMultiply) {
				return;
			}
			if (row.fxRate && row.glAmt) {
				if (mgl.fxMultiply == "Y") {
					row.glAmtB = arithService.mul(row.glAmt, row.fxRate);
				} else if (mgl.fxMultiply == "N" && row.fxRate != 0) {
					row.glAmtB = arithService.div(row.glAmt, row.fxRate);
				}
			} else if (row.glAmtB && row.glAmt) {
				if (mgl.fxMultiply == "Y" && row.glAmt != 0) {
					row.fxRate = arithService.div(row.glAmtB, row.glAmt);
				} else if (mgl.fxMultiply == "N" && row.glAmtB != 0) {
					row.fxRate = arithService.div(row.glAmt, row.glAmtB);
				}
			}
		}

		function glAmtBChanged(row, flag, mgl) {
			if (flag || !mgl.fxMultiply) {
				return;
			}
			if (row.glCcy && row.glCcy == mgl.baseCcy && row.glAmtB && row.fxRate) {
				if (mgl.fxMultiply == "Y" && row.fxRate != 0) {
					row.glAmt = arithService.div(row.glAmtB, row.fxRate);
				} else if (mgl.fxMultiply == "N") {
					row.glAmt = arithService.mul(row.glAmtB, row.fxRate);
				}
			} else if (row.glAmt && row.glAmtB) {
				if (mgl.fxMultiply == "Y" && row.glAmt != 0) {
					row.fxRate = arithService.div(row.glAmtB, row.glAmt);
				} else if (mgl.fxMultiply == "N" && row.glAmtB != 0) {
					row.fxRate = arithService.div(row.glAmt, row.glAmtB);
				}
			} else if (row.fxRate && row.glAmtB) {
				if (mgl.fxMultiply == "Y" && row.fxRate != 0) {
					row.glAmt = arithService.div(row.glAmtB, row.fxRate);
				} else if (mgl.fxMultiply == "N") {
					row.glAmt = arithService.mul(row.glAmtB, row.fxRate);
				}
			}
		}

		function fxRateChanged(row, flag, mgl) {
			if (flag || !mgl.fxMultiply) {
				return;
			}
			if (row.glAmt && row.fxRate) {
				if (mgl.fxMultiply == "Y") {
					row.glAmtB = arithService.mul(row.glAmt, row.fxRate);
				} else if (row.fxRate != 0) {
					row.glAmtB = arithService.div(row.glAmt, row.fxRate);
				}
			} else if (row.glAmtB && row.fxRate) {
				if (mgl.fxMultiply == "Y" && row.fxRate != 0) {
					row.glAmt = arithService.div(row.glAmtB, row.fxRate);
				} else if (mgl.fxMultiply == "N") {
					row.glAmt = arithService.mul(row.glAmtB, row.fxRate);
				}
			}
		}

		function acctCodechanged(row, firstLoad, mgl) {
			if (row) {
				if (firstLoad) {
					if (row.custAcctId) {
						row.custAcctFlag = "Y";
					} else {
						row.custAcctFlag = "N";
					}
				}
				if (row.custAcctFlag == "Y") {
					row._acctNoConfig = {
						model : "acctNo",
						type : "lookup-text",
						width : "12%",
						gridClass : "col-sm-12",
						inputClass : "input-sm",
						options : {
							"id" : "cls.lookupAcctNoList",
							"params" : {
								"comeFromId" : "mgl",
								"ccy" : "<glCcy",
								"custAcctType" : "<_custAcctNo",
								"custCode" : $filter("encode")(mgl.custCode),
								"crDr" : "<crDr"
							},
							"boundTo" : {
								"acctNo" : "acctNo",
								"custAcctId" : "custAcctId"
							},
							"okEvent" : true,
							"cancelEvent" : false,
							"manualInput" : false
						},
						uppercase : true
					};
					if (!firstLoad) {
						row.acctNo = null;
					}
				} else if (row.custAcctFlag == "N") {
					row._acctNoConfig = {
						model : "acctNo",
						type : "text",
						width : "12%",
						gridClass : "col-sm-12",
						inputClass : "input-sm",
						readonly : true
					};
					if (!firstLoad) {
						row.acctNo = row._custAcctNo;
					}
				}
			}
		}

		function glCcyChanged(row, flag, mgl) {
			if (flag) {
				return;
			}
			if (row.glCcy && row.glCcy == mgl.baseCcy) {
				row.fxRate = 1;
			}
			resetMglentrydetailsCustCode(mgl);
		}
		
		function resetMglentrydetailsCustCode(mgl) {
			if (mgl.mglentrydetails) {
				angular.forEach(mgl.mglentrydetails, function(row) {
					row.custCode = mgl.custCode;
					row.baseCcy = mgl.baseCcy;
					row.acctCode = "";
					row.acctNo = "";
					row._custAcctNo = "";
				});
			}
		}
		
		function addRow(row, mgl){
			return $q(function(resolved, rejected) {
				if (mgl.custCode) {
					row.custCode = mgl.custCode;
					row.baseCcy = mgl.baseCcy;
					resolved();
				} else {
					modalService.openErrorDialogs({
						"messages" : [ "error.mgl.customercodenotnull" ]
					});
					rejected();
				}
			});
		}

		return {
			initDataOptions : initDataOptions,
			resetMglentrydetails : resetMglentrydetails,
			glAmtChanged : glAmtChanged,
			glAmtBChanged : glAmtBChanged,
			fxRateChanged : fxRateChanged,
			acctCodechanged : acctCodechanged,
			glCcyChanged : glCcyChanged,
			addRow : addRow,
			resetMglentrydetailsCustCode : resetMglentrydetailsCustCode,
			getLoanInfoByLoanId : getLoanInfoByLoanId
		};
	}
})(angular);